<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      show-checkbox
      default-expand-all
      check-strictly
      :data="list"
      :props="{label:'name'}"
    />
    <el-row type="flex" justify="end">
      <el-button type="primary" size="mini" @click="hSubmit">确认</el-button>
      <el-button size="mini" @click="$emit('success')">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils/index'
import { assignPrem, findRoles } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPrem({ id: this.curId, permIds: permIds })
      this.$emit('success')
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        const detail = await findRoles(this.curId)
        console.log(detail)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        this.list = transDataToTreeData(res.data)
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
